package com.zhijian.medical.controller;

import com.zhijian.medical.annotation.CurrentUser;
import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.po.File;
import com.zhijian.medical.entity.vo.request.FileHistorySearchVo;
import com.zhijian.medical.entity.vo.request.FileSearchVo;
import com.zhijian.medical.entity.vo.request.FileUpdateVo;
import com.zhijian.medical.entity.vo.response.FileHistoryVo;
import com.zhijian.medical.entity.vo.response.FileVo;
import com.zhijian.medical.service.FileHistoryService;
import com.zhijian.medical.service.FileService;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件表 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-01-15 20:14:22
 */
@Slf4j
@Controller
@Api(tags = "文件表")
public class FileController {

    @Autowired
    private FileService service;

    @Autowired
    private FileHistoryService fileHistoryService;

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/file")
    @ApiOperation("新增")
    public boolean insert(@RequestBody File param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.insert(param) != null;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("/file/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("/file/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody FileUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/file/{id}")
    @ApiOperation("根据id查询")
    public FileVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/file/page")
    @ApiOperation("列表查询")
    public Page<FileVo> selectByCondition(@CurrentUser @ApiIgnore PersonDto personDto, @RequestBody(required = false) FileSearchVo param) {
        if (param == null)
            param = FileSearchVo.builder().build();
        param.setRoleIds(personDto.getRoleIds());
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 列表查询，不分页
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/file/list")
    @ApiOperation("列表查询，不分页")
    public List<FileVo> selectListByCondition(@CurrentUser @ApiIgnore PersonDto personDto, @RequestBody(required = false) FileSearchVo param) {
        if (param == null)
            param = FileSearchVo.builder().build();
        param.setRoleIds(personDto.getRoleIds());
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectList(param);
    }

    /**
     * 修改记录分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("/file/history/page")
    @ApiOperation("修改记录分页查询")
    public Page<FileHistoryVo> selectHistoryByCondition(@RequestBody FileHistorySearchVo param) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return fileHistoryService.selectPage(param);
    }

    /**
     * 学习调用
     *
     * @param id
     * @return
     */
    @Response
    @PostMapping("/file/study/{id}")
    @ApiOperation("学习调用")
    public boolean study(@PathVariable("id") Long id) {
        return service.study(id);
    }

    /**
     * 预览
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("/file/preview/{id}")
    @ApiOperation("预览")
    public void preview(@PathVariable("id") Long id, HttpServletResponse resp) {
        service.preview(id, resp);
    }

}